var express = require("express");
var router = express.Router();
var { addressModel } = require("../db/address_model");

// 获取地址
router.get("/address_list", async (req, res) => {
  console.log(req.query);
  const data = await addressModel.find().where({
    uid: req.query.uid,
  });
  total = data.length;
  if (data.length > 0) {
    res.send({
      code: 200,
      msg: "展示成功",
      data: data,
      total: total,
    });
  } else {
    res.send({
      code: 200,
      msg: "展示成功",
      data: [],
      total: total,
    });
  }
});
// 添加地址
router.post("/add_address", async (req, res) => {
  let body = req.body;
  console.log(`body`, body);
  await addressModel.create(body);
  res.send({
    code: 200,
    msg: "添加成功",
  });
});
// 修改地址
router.put("/edit_address/:id", async function (req, res) {
  let body = req.body;
  await addressModel.updateOne({ _id: req.params.id }, { $set: body });
  res.send({
    code: 200,
    msg: "修改成功",
  });
});

module.exports = router;
